package com.hymjweb.demo.hymj.master_bill_manage.other_charges.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.master_bill_manage.other_charges.bean.BMasterBillOtherChargePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;


@Mapper
@Component
public interface BMasterBillOtherChargeMapper {



    class BMasterBillOtherChargeMapperProvider{

        public String queryListByMasterBillId(String masterBillId){
            String sql= new SQL() {{
                SELECT("t.* ");
                FROM(" b_master_bill_other_charges t where 1=1 ");
            }}.toString();

            if(masterBillId!=null){
                sql += " and t.master_bill_id = " + DataTypeUtil.quoteStr(masterBillId);
            }

            return sql;
        }


        public String deleteByMasterBillId(String masterBillId){
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_other_charges t");
                WHERE("t.master_bill_id = #{masterBillId}");
            }}.toString();
            return sql;
        }

        public String delete(String id){
            String sql = new SQL() {{
                DELETE_FROM("b_master_bill_other_charges t");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String insert(BMasterBillOtherChargePojo pojo){
            String sql = new SQL() {{
                INSERT_INTO("b_master_bill_other_charges");
                VALUES("id","#{id}");
                VALUES("master_bill_type_id","#{masterBillTypeId}");
                VALUES("master_bill_id","#{masterBillId}");
                VALUES("name","#{name}");
                VALUES("amount","#{amount}");
                VALUES("comment","#{comment}");
            }}.toString();
            return sql;
        }
    }

    @Results(id = "otherChargeMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "master_bill_type_id", property = "masterBillTypeId"),
            @Result(column = "master_bill_id", property = "masterBillId"),
            @Result(column = "name", property = "name"),
            @Result(column = "amount", property = "amount"),
            @Result(column = "comment", property = "comment")
    })
    @SelectProvider(type = BMasterBillOtherChargeMapperProvider.class, method = "queryListByMasterBillId")
    List<BMasterBillOtherChargePojo> queryListByMasterBillId(String masterBillId);

    /**
     * 航空主单其他费用明细批量插入(b_master_bill_other_charges)
     * @param otherCharges 其他费用插入列表
     * @return 插入行数
     */
    @Insert(
            "<script>" +
                    "insert into b_master_bill_other_charges(id,master_bill_type_id,master_bill_id,name,amount,comment) values " +
                    "<foreach collection='otherCharges' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.masterBillTypeId},#{pojo.masterBillId},#{pojo.name},#{pojo.amount},#{pojo.comment})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsert(@Param("otherCharges") List<BMasterBillOtherChargePojo> otherCharges);


    @InsertProvider(type = BMasterBillOtherChargeMapperProvider.class, method = "insert")
    @Options(keyProperty = "id")
    int insert(BMasterBillOtherChargePojo otherChargePojo);

    @DeleteProvider(type = BMasterBillOtherChargeMapperProvider.class, method = "deleteByMasterBillId")
    int deleteByMasterBillId(String masterBillId);

    @DeleteProvider(type = BMasterBillOtherChargeMapperProvider.class, method = "delete")
    int delete(String id);



}
